package com.server.core.util;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.util.Properties;

/**
 * @desc 提供https的支持
 */
public class HttpsUtil {
    static  PropertiesUtil propertiesUtil = PropertiesUtil.getInstance();

    public static SSLContext getSslContext() throws Exception {
        char[] passArray = propertiesUtil.getProperty("SSL_PASSWORD").toCharArray();
        SSLContext sslContext = SSLContext.getInstance("TLSv1");
        KeyStore ks = KeyStore.getInstance("JKS");
        //加载keytool 生成的文件
        InputStream inputStream = HttpsUtil.class.getResourceAsStream("/CA/CA.jks");
        ks.load(inputStream, passArray);
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(ks, passArray);
        sslContext.init(kmf.getKeyManagers(), null, null);
        inputStream.close();
        return sslContext;
    }

    public static void main(String[] args) throws Exception {
        SSLContext context = getSslContext();
        System.out.println(context);
    }
}
